package com.xht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xht.domain.ResponseResult;
import com.xht.domain.entity.Video;
import com.xht.enums.AppHttpCodeEnum;
import com.xht.exception.SystemExceptin;
import com.xht.mapper.VideoMapper;
import com.xht.service.VideoService;
import com.xht.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * (Video)表服务实现类
 *
 * @author makejava
 * @since 2025-03-11 14:15:42
 */
@Service("videoService")
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    /**
     * 获取视频列表接口
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseResult videoList(Integer pageNum, Integer pageSize) {

        LambdaQueryWrapper<Video> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Video::getStatus, "0");
        Page<Video> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);

        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    /**
     * 获取视频详情
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public ResponseResult getVideoDetail(Integer id) {
        Video video = baseMapper.selectById(id);
        //访问量需要加一
        Long viewCount = video.getViewCount();
        video.setViewCount(++viewCount);
        //在修改数据库数据
        getBaseMapper().updateById(video);
        return ResponseResult.okResult(video);
    }

    /**
     * 后台--展示视频列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseResult GetVideoList(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Video> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Video::getStatus, "0");
        Page<Video> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);

        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    /**
     * 添加 视频
     *
     * @param video
     * @return
     */
    @Override
    public ResponseResult addVideo(Video video) {
        baseMapper.insert(video);
        return ResponseResult.okResult();
    }

    /**
     * 逻辑删除 视频
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteVideo(Integer id) {
        LambdaUpdateWrapper<Video> updateWrapper = Wrappers.lambdaUpdate(Video.class).eq(Video::getId, id)
                .set(Video::getDelFlag, "1");
        update(updateWrapper);
        return ResponseResult.okResult();
    }

    /**
     * 回显 视频信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getVideo(Integer id) {

        Video video = getBaseMapper().selectById(id);

        return ResponseResult.okResult(video);
    }

    /**
     * 更新 视频信息
     *
     * @param video
     * @return
     */
    @Override
    public ResponseResult updateVideo(Video video) {
        //需要检查 更新后的 有无  url 和 title
        String title = video.getTitle();
        String url = video.getUrl();
        if (!StringUtils.hasText(title) || !StringUtils.hasText(url)) {
            throw new SystemExceptin(AppHttpCodeEnum.TITLEORURL_NOT_NULL);
        }
        baseMapper.updateById(video);
        return ResponseResult.okResult();
    }
}

